package com.bugman.managed;

import com.bugman.business.BugService;
import com.bugman.business.FilterService;
import com.bugman.entity.Bug;
import com.bugman.entity.Importance;
import com.bugman.entity.Status;
import com.bugman.filters.BugFilter;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.model.SelectItem;

/**
 *
 * @author Ediczech
 */

public class BugTableBean {
    private Integer statusId;
    private Integer importanceId;
    private List<SelectItem> statuses = new ArrayList<SelectItem>();
    private List<SelectItem> importances = new ArrayList<SelectItem>();
    private BugFilter filter;

    final private Integer COUNT = 20;
    @EJB
    BugService bugService;
    @EJB
    FilterService filterService;

    @PostConstruct
    public void init() {
        filter = filterService.getBugFilter();
        importances.add(new SelectItem(0, "bez filtru"));
        for (Importance importance : bugService.getAllImportances()) {
            importances.add(new SelectItem(importance.getId(), importance.getName()));
        }
        statuses.add(new SelectItem(0, "bez filtru"));
        for (Status status : bugService.getAllStatuses()) {
            statuses.add(new SelectItem(status.getId(), status.getName()));
        }
    }

        public String filter() {
            Importance importance;
            if(importanceId==0) {
                importance = null;
            } else {
                importance = bugService.getImportance(importanceId.byteValue());
            }
            filter.setImportance(importance);
            
            Status status;
            if(statusId==0) {
                status = null;
            } else {
                status = bugService.getStatus(statusId.byteValue());
            }
            filter.setStatus(status);

        return "success";
    }

    public List<Bug> getBugs() {
        return bugService.getBugs(filter, COUNT);
    }

    public Integer getImportanceId() {
        return importanceId;
    }

    public void setImportanceId(Integer importanceId) {
        this.importanceId = importanceId;
    }

    public List<SelectItem> getImportances() {
        return importances;
    }

    public void setImportances(List<SelectItem> importances) {
        this.importances = importances;
    }

    public Integer getStatusId() {
        return statusId;
    }

    public void setStatusId(Integer statusId) {
        this.statusId = statusId;
    }

    public List<SelectItem> getStatuses() {
        return statuses;
    }

    public void setStatuses(List<SelectItem> statuses) {
        this.statuses = statuses;
    }

}
